<?php

namespace Biz\service\advertisement\apiModule;

use Doraemon\cache\PositionModule\ApiCounter;
use Doraemon\pockets\constInterface\PopupServiceConf;
use Doraemon\service\Popups\PopupsApi;
use Doraemon\service\Totem\TotemService;

class TabPopups extends BaseModule
{

    public function extListMap($ids)
    {
        return empty($ids) ? [] : PopupsApi::getListByRelateIds($ids);
    }

    public function extCheckActive($v, $extInput)
    {
        $tab = $extInput['tab'] ?? '-1';

        return $v['pos'] == $tab;
    }

    public function format($hit, $uid, $userProfileExt, $statSignExt)
    {

        $type = $hit['ext']['popups_type'] ?? 0;
        if (isset($hit['ext']['popup'])) $type = $hit['ext']['popup']['popups_type'] ?? 0;

        if ($type == PopupServiceConf::STYLE_CUSTOM) {
            return $hit ? ['type' => $type, 'content' => $hit] : null;
        } elseif ($type == PopupServiceConf::STYLE_SQUARE) {
            $hit = TotemService::totemPopupTab($uid);
            return $hit ?: null;
        } elseif ($type == PopupServiceConf::STYLE_CHECK_LINK) {
            $hit['statSign'] = $this->statSign($hit);
            return $hit ? ['type' => $type, 'content' => $hit] : null;
        } elseif ($type == PopupServiceConf::STYLE_LOTTIE) {
            $hit['statSign'] = $this->statSign($hit);
            return $hit ? ['type' => $type, 'content' => $hit] : null;
        }

        if ($this->position_id == 110) {
            $hit['statSign'] = $this->statSign($hit);
        }

        return $hit ? ['type' => $type, 'content' => PopupsApi::format($hit)] : null;
    }

    public function getExtInfo($baseInfo)
    {
        $id = $baseInfo['id'] ?? 0;

        //弹窗组件
        $popups = $id > 0 ? PopupsApi::get($id) : [];

        return ['popups' => $popups];
    }

    public function formatAdmListExtInfo(&$v, $map)
    {
        //预览链接
        $template = '{"type":"yapi","method":"post","uri":"/Popup/tab","params":{"preview":"%s"}}';
        $ext = $map[$v['id']] ?? [];
        $type = $ext['popups_type'];
        $v['linkPreview'] = vsprintf($template, $v['id']);
        $v['linkPreview'] = 'niceliving://api/execute?protego=' . urlencode(base64_encode($v['linkPreview']));
        $popups = PopupsApi::format(['ext' => $ext]);
        $v['img_url'] = $type == 3 ? $popups['img_url'] : (empty($popups['picArea']) ? $popups['backgroundArea']['pic_url'] : $popups['picArea']['pic_url']);
    }

}